Page({

    data: {

      /**
       * 标记点 需输入经纬度  
       */
       multiArray:[['2021年','2022年'],['01月','02月','03月','04月','05月','06月','07月','08月','09月','10月','11月','12月'],['01日','02日','03日','04日','05日','06日','07日','08日','09日','10日','11日','12日','13日','14日','15日','16日','17日','18日','19日','20日','21日','22日','23日','24日','25日','26日','27日','28日','29日','30日','31日'],['09时','10时','11时','12时','13时','14时','15时','16时','17时','18时','19时','20时','21时','22时','23时',]],
       arrivalIndex:[0,0,0,0],
       cancelIndexs:[0,0,0,0],
       money:[0,1,2,3,4,5,6,7,8,9,10],
       index:0,
       markers: [],
       markerIds:[],
       startLocation:"",
       endLocation:"",
       hasLocation:false,
       hasStartLocation:false, 
       hasEndLocation:false,
       showMyOrder:false,




    },
            //获取起始点函数
    getstartlocation:function(){  

      var that = this 



      wx.chooseLocation({success:function(start){ 
        that.setData({
          startLocation:start.name,
          hasStartLocation:true
        })
        
            //  新建数组将选择点的经纬放入数组
        var markers=[{
            id:1,
            width:40,
            height:40,
            latitude:start.latitude,
            longitude:start.longitude,
            iconPath:"../../icon/startflag.png"
         }]
         that.data.markers[0]=markers[0]
              //将起始点添加进markers中
         that.mapCtx.addMarkers({
           markers:markers,
           success(){
             console.log("添加起点成功")
           }
         })
      }
      })
                               },
            //获取终点函数
    getendlocation:function(){   
      var that = this
      wx.chooseLocation({success:function(end){ 
        console.log(end.address)
        //将用户终点信息赋值到endLocation上
        that.setData({
          endLocation:end.name,
          hasEndLocation:true,
          hasLocation:true
        })
            //新建数组将选择点的经纬放入数组
        var markers=[{
          id:2,
          width:40,
          height:40,
          latitude:end.latitude, 
          longitude:end.longitude,
          iconPath:"../../icon/endflag.png"
                 }]
            //将终点添加进markers中
        that.data.markers[1]=markers[0]
        that.mapCtx.addMarkers({
          markers:markers,
          success(){
            console.log("添加终点成功")
          }})
    }
    })
                              },



            //获取当前地图中心位置的经纬度
    // getCenterLocation:function () {    
    //   var that = this    
    //     that.mapCtx.getCenterLocation({
    //         success: function(res){
    //           console.log(res.longitude)
    //           console.log(res.latitude)

    //         }
    //       })

    //                               },

    //发布订单                            
    issueOrder:function(){
      var that = this
      that.setData({
        hasLocation:false,
        showMyOrder:true
      })
      wx.request({
      // url: 'http://localhost:10002/car/publish',
      url:'http://119.23.252.15:10002/car/publish',
      method:'POST', 
      data:{
        // "token": wx.getStorageSync({key:'token', success(){console.log("读取用户token失败")}}),
        token:wx.getStorageSync('token'),
        "type": "car",
        "order": {
          "reward": that.data.index,
          "startPoint": that.data.startLocation,
          "destination": that.data.endLocation,
          "expectedDate": that.data.multiArray[0][that.data.arrivalIndex[0]]+that.data.multiArray[1][that.data.arrivalIndex[1]]+that.data.multiArray[2][that.data.arrivalIndex[2]]+that.data.multiArray[3][that.data.arrivalIndex[3]],
          "cancelDate": that.data.multiArray[0][that.data.cancelIndexs[0]]+that.data.multiArray[1][that.data.cancelIndexs[1]]+that.data.multiArray[2][that.data.cancelIndexs[2]]+that.data.multiArray[3][that.data.cancelIndexs[3]],
          "locationMarkers": JSON.stringify(that.data.markers)
        },
        success(){
          console.log("发布订单成功|task-"+that.data.markers+"|reward-"+that.index+"|targetSite-"+that.data.startLocation)
          console.log("期望时间-"+that.data.multiArray[0][that.data.arrivalIndex[0]]+that.data.multiArray[1][that.data.arrivalIndex[1]]+that.data.multiArray[2][that.data.arrivalIndex[2]]+that.data.multiArray[3][that.data.arrivalIndex[3]])
          console.log("订单取消时间-"+that.data.multiArray[0][that.data.cancelIndexs[0]]+that.data.multiArray[1][that.data.cancelIndexs[1]]+that.data.multiArray[2][that.data.cancelIndexs[2]]+that.data.multiArray[3][that.data.cancelIndexs[3]])
        }
        
      }
    })
        wx.showToast({
          title: '发布订单成功',
    })
          // console.log("发布订单成功|targetSite-"+that.data.startLocation)
          // console.log(JSON.stringify(that.data.markers))
          // console.log("期望时间-"+that.data.multiArray[0][that.data.arrivalIndex[0]]+that.data.multiArray[1][that.data.arrivalIndex[1]]+that.data.multiArray[2][that.data.arrivalIndex[2]]+that.data.multiArray[3][that.data.arrivalIndex[3]])
          // console.log("订单取消时间-"+that.data.multiArray[0][that.data.cancelIndexs[0]]+that.data.multiArray[1][that.data.cancelIndexs[1]]+that.data.multiArray[2][that.data.cancelIndexs[2]]+that.data.multiArray[3][that.data.cancelIndexs[3]])

    },

    //跳转至我的订单
    navigato(e){
      console.log("跳转至我的订单")
      this.mapCtx.removeMarkers({
        markerIds:[1,2]
      })
      this.setData({
        startLocation:"",
        endLocation:"",
        hasLocation:false,
        hasStartLocation:false, 
        hasEndLocation:false,
        showMyOrder:false,
      })
      this.onLoad()
      wx.navigateTo({
        url: '/pages/mycarorders/mycarorders',
      })
  },

    //跳转至接单中心
  goToTakeCarOrder(){
    wx.navigateTo({
      url: '/pages/CarOrder/CarOrder',
    })
  },

  //检测赏金发生变化
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },

  //检测期待时间是否发生变化
  arrivalIndexPickerChange: function (e) {
    this.setData({
      arrivalIndex: e.detail.value
    })
  },

  //检测订单取消时间是否发生变化
  cancelIndexPickerChange: function(e) {
    this.setData({
      cancelIndexs: e.detail.value
    })
   },
            

    onLoad() {
        let that = this
        //创建对象id
        that.mapCtx = wx.createMapContext('myMap')
        that.setData({ 
          index:0,
          arrivalIndex:[0,0,0,0],
          cancelIndexs:[0,0,0,0],
          markers:[],
          markerIds:[],
          startLocation:"",
          endLocation:"",
          hasLocation:false,
          hasStartLocation:false, 
          hasEndLocation:false,
          showMyOrder:false,
        })
        //用户授权
        wx.authorize({scope: 'scope.userLocationBackground'})
        wx.authorize({scope: 'scope.userLocation'})         
        //将地图中心移至当前定位点
        that.mapCtx.moveToLocation()
        //开启定位服务
        // wx.startLocationUpdateBackground({
        //     success:function(res){
        //位置改变则返回当前位置经纬度       
              // wx.onLocationChange(function (result){
            //  console.log(result)
            //  console.log("速度为:"+result.speed+" 经纬"+result.latitude)
             //定义markers点 
            //  var markers=[{
            //    id:0,
            //    width:25,
            //    height:25,
            //    latitude:result.latitude,
            //    longitude:result.longitude,
            //    iconPath:"../../icon/home-o.png"
            //  }]
            //  //定义移除的marker的id
            //  var markerIds = [0]
            //  //移除指定的marker
            //  that.mapCtx.removeMarkers({
            //  markerIds:markerIds
            //  })
            //  console.log("remove")
            //  //将定义的marker点添加进markers中
            //  that.mapCtx.addMarkers({
            //    markers:markers
            //  })
                                                  // })
        //                          }
        // })
        },



        
      }) 
